package com.sgb.jicai.view.activity;

import android.content.Context;
import android.content.Intent;
import androidx.databinding.DataBindingUtil;
import android.graphics.Typeface;
import android.os.Bundle;
import com.google.android.material.tabs.TabLayout;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;
import com.sgb.jicai.R;
import com.sgb.jicai.databinding.ActivityJcMyPriceBinding;
import com.sgb.jicai.event.JiCaiEventEntity;
import com.sgb.jicai.view.fragment.JcMyPriceFragment;
import com.sgb.jicai.viewmodel.JcMyPriceModel;
import com.sgb.link.entity.EventConstant;
import com.sgb.link.view.adapter.PickNoticeAdapter;
import com.swgk.core.base.AppActivity;
import com.swgk.core.util.PickSelectEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.recyclerview.adapter.SlidingFragmentPagerAdapter;
import com.swgk.core.util.SharedPreferenceUtil;
import org.greenrobot.eventbus.EventBus;
import java.util.ArrayList;
import java.util.List;

/**
 * 个人中心——集采管理——我的报价
 */
public class JcMyPriceActivity extends AppActivity {

    JcMyPriceModel model;
    private ActivityJcMyPriceBinding binding;
    private PickNoticeAdapter pagerAdapter;
    private ArrayList<Fragment> mFragments;
    private ArrayList<String> titles = new ArrayList<>();
    //一级tab数据
    private List<PickSelectEntity> dataTabFirst;

    public static void start(Context context) {
        Intent intent = new Intent(context, JcMyPriceActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_jc_my_price);
        model = new ViewModelProvider(this).get(JcMyPriceModel.class);
        model.setActivity(JcMyPriceActivity.this);
        binding.ivBack.setOnClickListener(this::onMultiClick);
        binding.tvSearch.setOnClickListener(this::onMultiClick);
        String companyNo = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.COMPANY_NO, "");
        if (companyNo.equals("") || companyNo.equals("0")) {
            //个人
            binding.tabLayout.setVisibility(View.GONE);
            binding.viewpager.setNoScroll(true);
        } else {
            //公司
            binding.tabLayout.setVisibility(View.VISIBLE);
            binding.viewpager.setNoScroll(false);
        }
    }

    @Override
    protected void init() {
        //使用动态获取的分类//        model.groupBuyClassList();  1.12改版后不适用
        model.getTypeList();
        binding.editSearch.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {
//                    if (TextUtils.isEmpty(binding.etView.getText().toString())) {
//                        MToast.showToast(JcMyPriceActivity.this, "请输入关键字");
//                        return true;
//                    }
                    //调用Fragment的刷新
                    EventBus.getDefault().post(new JiCaiEventEntity(EventConstant.EVENT_JICAI_MY_PRICE, binding.editSearch.getText().toString().trim()));
                    hideKeyboard();
                }
                return false;
            }
        });
    }

    public void updateViews(List<PickSelectEntity> data) {
        if (data == null || data.size() == 0) {
            DialogHelper.dismissProgressDialog();
            showEmpty();
            return;
        }
        dataTabFirst = data;
        String[] mTitles = new String[data.size()];
        ArrayList<Fragment> mFragments = new ArrayList<>();
        for (int i = 0; i < data.size(); i++) {
            mTitles[i] = data.get(i).getProjectscale();
            mFragments.add(JcMyPriceFragment.newInstance(data.get(i).getNo().isEmpty()?0:Integer.valueOf(data.get(i).getNo()), 0));
        }
        SlidingFragmentPagerAdapter mPagerAdapter = new SlidingFragmentPagerAdapter(getSupportFragmentManager(), mFragments, mTitles);
        binding.viewpager.setOffscreenPageLimit(mTitles.length);
        binding.viewpager.setAdapter(mPagerAdapter);
        binding.tabLayout.setupWithViewPager(binding.viewpager);
        binding.tabLayout.setTabMode(data.size() > 4 ? TabLayout.MODE_SCROLLABLE : TabLayout.MODE_FIXED);

        for (int i = 0; i < binding.tabLayout.getTabCount(); i++) {
            TabLayout.Tab tab = binding.tabLayout.getTabAt(i);
            if (tab != null) {
                tab.setCustomView(getTabView(i));
            }
        }
        updateTabTextView(binding.tabLayout.getTabAt(binding.tabLayout.getSelectedTabPosition()), true);
        binding.tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                updateTabTextView(tab, true);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                updateTabTextView(tab, false);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
            }
        });
    }

    private View getTabView(int currentPosition) {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.tab_item_textview, null);
        TextView textView = (TextView) view.findViewById(R.id.tv_tab_item_textview);
        textView.setTextColor(getResources().getColor(R.color.color_main_bg));
        textView.setTextSize(15);
        textView.setText(dataTabFirst.get(currentPosition).getPickerViewText());
        return view;
    }

    private void updateTabTextView(TabLayout.Tab tab, boolean isSelect) {
        if (tab == null || tab.getCustomView() == null) return;
        if (isSelect) {
            //选中加粗
            TextView tabSelect = (TextView) tab.getCustomView().findViewById(R.id.tv_tab_item_textview);
            tabSelect.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
            tabSelect.setTextColor(getResources().getColor(R.color.white));
            tabSelect.setTextSize(18);
            tabSelect.setText(tab.getText());
        } else {
            TextView tabUnSelect = (TextView) tab.getCustomView().findViewById(R.id.tv_tab_item_textview);
            tabUnSelect.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
            tabUnSelect.setTextColor(getResources().getColor(R.color.color_main_bg));
            tabUnSelect.setTextSize(15);
            tabUnSelect.setText(tab.getText());
        }
    }

    public void showEmpty() {
        binding.emptyView.setVisibility(View.VISIBLE);
    }

    @Override
    public void onMultiClick(View v) {
        super.onMultiClick(v);
        int id = v.getId();
        if (id == R.id.iv_back) {
            finish();
        } else if (id == R.id.tv_search) {//调用Fragment的刷新
            EventBus.getDefault().post(new JiCaiEventEntity(EventConstant.EVENT_JICAI_MY_PRICE, binding.editSearch.getText().toString().trim()));
            hideKeyboard();
        } else if (id == R.id.tv_search_delete) {
            binding.editSearch.setText("");
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        hideKeyboard();
    }

    /**
     * 获取InputMethodManager，隐藏软键盘
     */
    private void hideKeyboard() {
        InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
        // 隐藏软键盘
        imm.hideSoftInputFromWindow(this.getWindow().getDecorView().getWindowToken(), 0);
    }


}
